package com.imageFilters;

import android.graphics.Bitmap;

/**
 * 老照片特效
 * @author Administrator
 *
 */
public class OldPhotoFilter implements ImageFilterInterface{
	private ImageData imageIn;

	
	public OldPhotoFilter() {
		super();
	}
	
	public ImageData imageProcess(Bitmap bmp) {
		this.imageIn = new ImageData(bmp);
		return imageProcess();
	}
	/**
	 * Sets the RGB between 0 and 255
	 *
	 * @param a
	 * @return
	 */
	private int truncate(int a) {
		if (a < 0)
			return 0;
		else if (a > 255)
			return 255;
		else
			return a;
	}
	public ImageData imageProcess() {
		int width = imageIn.getWidth();
		int height = imageIn.getHeight();
        int pixR = 0;  
        int pixG = 0;  
        int pixB = 0;  
        int newR = 0;  
        int newG = 0;  
        int newB = 0;  
        for (int x = 0; x < width; x++)  
        {  
            for (int y = 0; y < height; y++)
            {  
                pixR = imageIn.getRComponent(x, y);
                pixG = imageIn.getGComponent(x, y);
                pixB = imageIn.getBComponent(x, y);
                newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);  
                newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);  
                newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);  
                imageIn.setPixelColor(x, y, truncate(newR), truncate(newG), truncate(newB));
            }  
        }  
		return imageIn;
	}

}
